#include <stdio.h>
#include <iconv.h>
#include <locale.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>

int main(int argc, char * argv[])
{
    int ret;
    char str[10] = {0};
    size_t src_len;
    size_t dest_len;
    char *dest;
    iconv_t conv;

    strcpy(str, "中文");

    src_len = strlen(str);
    dest_len = src_len * 2;
    
    dest = (char*)malloc(dest_len);
    bzero(dest, dest_len);

    /**
     * UCS-2LE  Little-endian
     * UCS-2BE  Big-endian
     * iconv函数线程非安全
     */
    //conv = iconv_open("UCS-2LE", "UTF-8");
    conv = iconv_open("UCS-2BE", "UTF-8");
    if (conv == (iconv_t) - 1) {
        printf("iconv_open: %d\n", errno);
        return -1;
    }
    char* s = str;
    char* d = dest;
    ret = iconv(conv, (char**)&s, &src_len, &d, &dest_len); 
    iconv_close(conv);
    if (ret == -1) {
      printf("iconv: %d\n", errno);
      return -1;
    }
    printf("%s - %s\n", str, dest);

    free(dest);
    
    return 0;
}
